﻿using UniRx;
using UnityEngine;
using System;

namespace QFramework
{
    public interface IEntity
    {
        IEntity OnValueChange(Action<string, string> func);
        IEntity Begin(string Layer = "Normal");
        void SetState(string id, string Layer = "Normal");
    }

    public abstract class Entity:IEntity
    {
        private FSMLayer                                            m_FsmLayer;
        private IDisposable                                         m_Disposable;
        private Transform                                           m_User;
        protected GameObject                                        self;

        private Action<string,string>                               m_ValueChnageFunc;
        
        public Entity(GameObject SelfObj)
        {
            m_FsmLayer = new QFramework.FSMLayer();
            InitState();
            self = SelfObj;
            if (m_Disposable != null)
                m_Disposable.Dispose();

            m_Disposable = Observable.EveryUpdate()
                .Do(x => m_FsmLayer.update())
                .DoOnCancel(() => Destroy())
                .Subscribe()
                .AddTo(SelfObj);
        }

        public Entity()
        {
            m_FsmLayer = new QFramework.FSMLayer();
            InitState();

            if (m_Disposable != null)
                m_Disposable.Dispose();

            m_Disposable = Observable.EveryUpdate()
                .Do(x => m_FsmLayer.update())
                .Subscribe();
        }

        /// <summary>
        /// 添加状态
        /// </summary>
        protected void AddState(State m_State)
        {
            if (m_FsmLayer == null)
                return;

            m_FsmLayer.AddState(m_State);
        }

        /// <summary>
        /// 开始状态机
        /// </summary>
        public virtual IEntity Begin(string Layer = "Normal")
        {
            if (m_FsmLayer == null)
                return null;

            m_FsmLayer.Start(Layer);
            return this;
        }

        /// <summary>
        /// 重置状态
        /// </summary>
        public void RestFsm()
        {
            m_FsmLayer.Rest();
        }

        /// <summary>
        /// 状态切换回调
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public IEntity OnValueChange(Action<string, string> func)
        {
            m_ValueChnageFunc = func;
            return this;
        }

        /// <summary>
        /// 初始化状态
        /// </summary>
        protected abstract void InitState();

        /// <summary>
        /// 手动卸载或传递Transform跟随Transform周期
        /// </summary>
        public virtual void Destroy()
        {
            Debug.Log("卸载");

            if (m_Disposable != null)
                m_Disposable.Dispose();

            m_FsmLayer.Close();
            m_FsmLayer = null;

            OnDestroy();
        }

        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="Layer"></param>
        public void SetState(string id, string Layer = "Normal")
        {
            if (m_FsmLayer == null)
                return;

            m_FsmLayer.SetTransition(id, Layer);
            if (m_ValueChnageFunc != null)
                m_ValueChnageFunc(id, m_FsmLayer.CurrentID(Layer));
        }

        /// <summary>
        /// 卸载回调
        /// </summary>
        protected abstract void OnDestroy();
    }

}
